
$(document).ready(function(){
	var testId=getvl("id");
	if(testId!="" && testId!=undefined){
			loadData(testId);
	}

	
});


function loadData(testId){
	 $.ajax({
        url:  urlcore + "/api/test/getTestById?testId="+testId,
        type: "GET",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success:function(data){
            if (data.success == true) {
            	var da=data.data;
            	var index=0
            	var contentArr=da.selectOption.split(",");
            	var rightOptionArr=da.rightOption.split(",");
            	var isMore=da.isMore;
            	var score=da.score;
               $("#title").val(da.title);
               for(var i=0;i<contentArr.length;i++){
               	if(i==0){
               		   	$("#content_1").val(contentArr[i]);
               		   	$("#cb_1").val(contentArr[i]);
               	}else if(i==1){
               		$("#content_2").val(contentArr[i]);
               		$("#cb_2").val(contentArr[i]);
               	}else if(i==2){
               		$("#content_3").val(contentArr[i]);
               		$("#cb_3").val(contentArr[i]);
               	}else if(i==3){
               			$("#content_4").val(contentArr[i]);
               			$("#cb_4").val(contentArr[i]);
               	}else if(i=4){
               		$("#content_5").val(contentArr[i]);
               		$("#cb_5").val(contentArr[i]);
               	}
               }
               for(var y=0;y<rightOptionArr.length;y++){
               	$("input:checkbox[value="+rightOptionArr[y]+"]").attr('checked','true');
               }
               $("#score").val(score);
             	$("input:radio[value="+isMore+"]").attr('checked','true');
               
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
	
}



//上传试题
function upTest(){
	var testId=getvl("id");
	var title=$("#title").val();
	var content_1=$("#content_1").val();
	var content_2=$("#content_2").val();
	var content_3=$("#content_3").val();
	var content_4=$("#content_4").val();
	var content_5=$("#content_5").val();
	var rightSelect="",select=""; 
	if($("#content_1").val()!="" && $("#content_1").val()!=undefined){
		select+=$("#content_1").val()+",";
	}
	if($("#content_2").val()!="" && $("#content_2").val()!=undefined){
		select+=$("#content_2").val()+",";
	}
	if($("#content_3").val()!="" && $("#content_3").val()!=undefined){
		select+=$("#content_3").val()+",";
	}
	if($("#content_4").val()!="" && $("#content_4").val()!=undefined){
		select+=$("#content_4").val()+",";
	}
	if($("#content_5").val()!="" && $("#content_5").val()!=undefined){
		select+=$("#content_5").val()+",";
	}
	select=select.substring(0,select.length-1) ;
	   if($("#cb_1").get(0).checked){
	   	if($("#content_1").val()==""){
	   		alert("正确选项内容不能为空！");
	   		return;
	   	}else{
	   		rightSelect+=$("#content_1").val()+",";
	   	}
	  
	   }
     if($("#cb_2").get(0).checked){
 	 	if($("#content_2").val()==""){
	   		alert("正确选项内容不能为空！");
	   		return;
	   	}else{
	   		rightSelect+=$("#content_2").val()+",";
	   	}
	   
	   }
      if($("#cb_3").get(0).checked){
  	 	if($("#content_3").val()==""){
	   		alert("正确选项内容不能为空！");
	   		return;
	   	}else{
   			rightSelect+=$("#content_3").val()+",";
	   	}
	   
	   }
       if($("#cb_4").get(0).checked){
	 	if($("#content_4").val()==""){
	   		alert("正确选项内容不能为空！");
	   		return;
	   	}else{
	   		rightSelect+=$("#content_4").val()+",";
	   	}
	   	
	   }
        if($("#cb_5").get(0).checked){
 		 	if($("#content_5").val()==""){
	   		alert("正确选项内容不能为空！");
	   		return;
	   	}else{
	   		rightSelect+=$("#content_5").val()+",";
	   	}
	   	
	   }
      rightSelect=rightSelect.substring(0,rightSelect.length-1) ;
      
	var isMore=$("input[name='rd']:checked").val();
	var score=$("#score").val();
	if(title==""){
		alert("请输入题目");
		return;
	}
	if(score==""){
		alert("请输入分数");
		return;
	}
	if(select==""){
		alert("请填写选项");
		return;
	}
	if(rightSelect==""){
		alert("请选择正确选项");
		return;
	}
	if(isMore=="" || isMore==undefined){
		alert("请选择多选类型");
		return;
	}
	if(rightSelect.indexOf(",")!=-1 && isMore==0){
		alert("对不起，本题为多选题");
		return;
	}else if(rightSelect.indexOf(",")==-1 && isMore==1){
		alert("对不起，本题为单选题");
		return;
	}
 $.ajax({
        url:  urlcore + "/api/test/addTest",
        type: "post",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
        	"id":testId,
            "title": title,
            "selectOption": select,
            "rightOption":rightSelect,
            "isMore":isMore,
            "score":score
        }),
        dataType: "json",
        success:function(data){
            if (data.success == true) {
            	var str=""
            	if(testId!=""){
            		alert("修改成功！")
            		 window.location.href = "test_list.html";
            	}else {
            		str="添加成功，是否继续添加？";
            		 if (confirm(str)) {
                        //刷新
                        location.reload();
                    } else {
                        //跳转列表页
                        window.location.href = "test_list.html";
                    }
            	}
                   
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
	
	
	
	
}
